$(document).ready(function () {
    var row = 16;
    var col = 36;
    var randsz;
    var position_this;       //当前位置
    var hasEnergy=100;      //判断是否有体力
    makeBase(row,col);
    $('.row .col-sm-1').addClass('chuzi');


    //单个宝物测试
    var treasure1 = makeTreasure();
    var t1x = [];
    var t1y = [];
    var start_x = randomNum(col-treasure1.w);
    var start_y = randomNum(row-treasure1.h);
    /*console.log(treasure1);*/
    console.log(start_x,start_y);
    for(var i =0;i<treasure1['w'];i++){
        t1x.push(start_x+i);
    }
    for(var j=0;j<treasure1['h'];j++){
        t1y.push(start_y+j);
    }
    var treasure = [];
    for(var i =start_x;i<treasure1['w']+start_x;i++){
        for(var j=start_y;j<treasure1['h']+start_y;j++){
            treasure.push([i,j]);
        }
    }
    console.log(treasure);

    /*console.log(t1x);
    console.log(t1y);*/
    $.each($('.row .col-sm-1'),function(){
        var x = parseInt($(this).data('i'));
        var y = parseInt($(this).data('j'));
        if(in_array([y,x],treasure)){
            var ty = y-start_x+1;
            var tx = x-start_y+1;
            $(this).addClass('treasure1_'+tx+'_'+ty);
            $(this).addClass('treasure1');
        }
        /*if(in_array(y, t1x)){
            if(in_array(x, t1y)){
                $(this).addClass('treasure1');
            }
        }*/
    });


    /**
     * 生成地形
     * @param row
     * @param col
     */
    function makeBase(row,col){
       var cons = '';
        for(var i=0;i<row;i++){
            cons+='<div class="row">';

            for(var k=0;k<col;k++){
                randsz = randomNum(1,5);
                cons2 = '<div class="col-sm-1 rock'+randsz+' mineSquare" data-i="'+i+'" data-j="'+k+'"></div>';
                cons+=cons2;
            }
            cons+='</div>';
            $('#mineBody').html(cons);
        }
    }


    /**
     * 生成宝物
     * @returns {{w: (*|number), h: (*|number)}}
     */
    function makeTreasure(){
        var treasure = {
            'w':5,
            'h':4
        };
        /*var wid = randomNum(1,4);
        var hei = randomNum(1,4);
        return {"w":wid,"h":hei};*/
        return treasure;
    }

    /*生成m-n之间的随机数*/
    function randomNum(minNum,maxNum){
        switch(arguments.length){
            case 1:
                return parseInt(Math.random()*minNum+1,10);
                break;
            case 2:
                return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
                break;
            default:
                return 0;
                break;
        }
    }

    /**
     * 挖宝事件
     */
    $('.row .col-sm-1').click(function(){
        if($(this).hasClass('rock0') || $(this).hasClass('treasure')){
            return;
        }
        if($(this).hasClass('chuzi')){
            if(hasEnergy > 0){
                hasEnergy = damage(1);
                showSound("./audio/chuzi.mp3");
                var cengclass = $(this)[0].className.split(/\s+/);
                cengclass = cengclass.join(',');
                var a=cengclass.indexOf("rock");
                cengclass = cengclass.substr(a,5);

                var ceng = cengclass.substr(cengclass.length-1,1);

                if(ceng==1 && $(this).hasClass('treasure1')){
                    $(this).removeClass('rock1').addClass('treasure');
                }else{
                    if(ceng>0){
                        ceng = ceng-1;
                        $(this).removeClass(cengclass).addClass('rock'+ceng);
                    }else{
                        $(this).removeClass(cengclass).addClass('rock'+ceng);
                    }
                }
            }
        }else{
            if(hasEnergy > 2){
                hasEnergy = damage(3);
                showSound("./audio/chuizi.mp3");
                var position = [];
                var x = $(this).data('i');
                var y = $(this).data('j');
                position.push([x,y]);
                position.push([x-1,y]);
                position.push([x+1,y]);
                position.push([x,y+1]);
                position.push([x,y-1]);

                $.each($('.row .col-sm-1'),function(){
                    var x = $(this).data('i');
                    var y = $(this).data('j');
                    position_this = [x,y];
                    if(arrayHasElement(position,position_this)){
                        var cengclass = $(this)[0].className.split(/\s+/);
                        cengclass = cengclass.join(',');
                        var a=cengclass.indexOf("rock");
                        cengclass = cengclass.substr(a,5);

                        var ceng = cengclass.substr(cengclass.length-1,1);
                        if(ceng==1 && $(this).hasClass('treasure1')){
                            $(this).removeClass('rock1').addClass('treasure');
                        }else{
                            if(ceng>0){
                                ceng = ceng-1;
                                $(this).removeClass(cengclass).addClass('rock'+ceng);
                            }else{
                                $(this).removeClass(cengclass).addClass('rock'+ceng);
                            }
                        }

                        /*console.log(position_this);*/
                    }
                });
            }

        }
        //判断是否被完整挖出
        var count = 0;
        $('.treasure1').each(function () {
            if($(this).hasClass('treasure')){count++;}
            if(count == $('.treasure1').length){layer.msg('挖出来了!');}
            $('.row>h3>span').html(1);
        });
        /*console.log('当前数量:'+count);*/

    });


    /**
     * @param {number[][]} array
     * @param {number[]} element
     * @return {boolean}
     *  判断二维数组array中是否存在一维数组element
     */
    var arrayHasElement = function(array, element) {
        for (var el of array) {
            if (el.length === element.length) {
                for (var index in el) {
                    if (el[index] !== element[index]) {
                        break;
                    }
                    if (index == (el.length - 1)) {    // 到最后一个元素都没有出现不相等，就说明这两个数组相等。
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * @param stringToSearch
     * @param arrayToSearch
     * @returns {boolean}
     * 判断字符串是否在数组中
     */
    function in_array(stringToSearch, arrayToSearch) {
        for (s = 0; s < arrayToSearch.length; s++) {
            thisEntry = arrayToSearch[s].toString();
            if (thisEntry == stringToSearch) {
                return true;
            }
        }
        return false;
    }

    function showSound(audioSrc) {
        /**因为音效元素是追加的，所以每次生成之前，将原来的删除掉*/
        $("#hint").remove();
        /**创建audio标签的Jquery对象，然后追加到body进行播放即可*/
        let audioJQ = $("<audio src='" + audioSrc + "' autoplay id='hint'/>");
        audioJQ.appendTo("body");
        var x = document.getElementById("hint");
        x.volume = 0.2;
    }


    /**
     * 体力调整
     * @param dmg
     */
    function damage(dmg){
        var bar = document.getElementById("bar");
        var total = document.getElementById("total");
        if(parseInt(bar.style.width)>dmg){
            bar.style.width=parseInt(bar.style.width) - dmg + "%";
            /*var timeout=window.setTimeout("damage(Math.ceil(Math.random()*10))",200);*/
        }else{
            bar.style.width= '0%';
        }

        total.innerHTML = bar.style.width;
        /*if(bar.style.width == "0%"){
            alert('died');
            /!*bar.style.width = "100%";*!/
        }*/
        return bar.style.width.substr(0,bar.style.width.length-1);
    }
});

